package com.castle.fortress.admin.study.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.study.exam.dto.StudyQuestionbankTypeDto;
import com.castle.fortress.admin.study.exam.entity.StudyQuestionbankEntity;
import com.castle.fortress.admin.study.exam.entity.StudyQuestionbankTypeEntity;
import com.castle.fortress.admin.study.exam.service.StudyQuestionbankService;
import com.castle.fortress.admin.study.exam.service.StudyQuestionbankTypeService;
import com.castle.fortress.admin.utils.ExcelUtils;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 题库分类 控制器
 *
 * @author castle
 * @since 2023-02-09
 */
@Api(tags="题库分类管理控制器")
@Controller
public class StudyQuestionbankTypeController {
    @Autowired
    private StudyQuestionbankTypeService studyQuestionbankTypeService;
    @Autowired
    private StudyQuestionbankService studyQuestionbankService;

    /**
     * 题库分类的分页展示
     * @param studyQuestionbankTypeDto 题库分类实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="题库分类",operType = OperationTypeEnum.QUERY)
    @ApiOperation("题库分类分页展示")
    @GetMapping("/study/exam/studyQuestionbankType/page")
    @ResponseBody
    @RequiresPermissions("study:exam:studyQuestionbankType:pageList")
    public RespBody<IPage<StudyQuestionbankTypeDto>> pageStudyQuestionbankType(StudyQuestionbankTypeDto studyQuestionbankTypeDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<StudyQuestionbankTypeDto> page = new Page(pageIndex, pageSize);
        IPage<StudyQuestionbankTypeDto> pages = studyQuestionbankTypeService.pageStudyQuestionbankType(page, studyQuestionbankTypeDto);

        return RespBody.data(pages);
    }

    /**
     * 题库分类的列表展示
     * @param studyQuestionbankTypeDto 题库分类实体类
     * @return
     */
    @CastleLog(operLocation="题库分类",operType = OperationTypeEnum.QUERY)
    @ApiOperation("题库分类列表展示")
    @GetMapping("/study/exam/studyQuestionbankType/list")
    @ResponseBody
    public RespBody<List<StudyQuestionbankTypeDto>> listStudyQuestionbankType(StudyQuestionbankTypeDto studyQuestionbankTypeDto){
        List<StudyQuestionbankTypeDto> list = studyQuestionbankTypeService.listStudyQuestionbankType(studyQuestionbankTypeDto);
        return RespBody.data(list);
    }

    /**
     * 题库分类保存
     * @param studyQuestionbankTypeDto 题库分类实体类
     * @return
     */
    @CastleLog(operLocation="题库分类",operType = OperationTypeEnum.INSERT)
    @ApiOperation("题库分类保存")
    @PostMapping("/study/exam/studyQuestionbankType/save")
    @ResponseBody
    @RequiresPermissions("study:exam:studyQuestionbankType:save")
    public RespBody<String> saveStudyQuestionbankType(@RequestBody StudyQuestionbankTypeDto studyQuestionbankTypeDto){
        if(studyQuestionbankTypeDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyQuestionbankTypeEntity studyQuestionbankTypeEntity = ConvertUtil.transformObj(studyQuestionbankTypeDto,StudyQuestionbankTypeEntity.class);
        if(studyQuestionbankTypeService.checkName(studyQuestionbankTypeEntity)){
            throw new BizException(BizErrorCode.NAME_EIXST_ERROR);
        }
        if(studyQuestionbankTypeService.save(studyQuestionbankTypeEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 题库分类编辑
     * @param studyQuestionbankTypeDto 题库分类实体类
     * @return
     */
    @CastleLog(operLocation="题库分类",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("题库分类编辑")
    @PostMapping("/study/exam/studyQuestionbankType/edit")
    @ResponseBody
    @RequiresPermissions("study:exam:studyQuestionbankType:edit")
    public RespBody<String> updateStudyQuestionbankType(@RequestBody StudyQuestionbankTypeDto studyQuestionbankTypeDto){
        if(studyQuestionbankTypeDto == null || studyQuestionbankTypeDto.getId() == null || studyQuestionbankTypeDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyQuestionbankTypeEntity studyQuestionbankTypeEntity = ConvertUtil.transformObj(studyQuestionbankTypeDto,StudyQuestionbankTypeEntity.class);
        if(studyQuestionbankTypeService.checkName(studyQuestionbankTypeEntity)){
            throw new BizException(BizErrorCode.NAME_EIXST_ERROR);
        }
        if(studyQuestionbankTypeService.updateById(studyQuestionbankTypeEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 题库分类删除
     * @param id
     * @return
     */
    @CastleLog(operLocation="题库分类",operType = OperationTypeEnum.DELETE)
    @ApiOperation("题库分类删除")
    @PostMapping("/study/exam/studyQuestionbankType/delete")
    @ResponseBody
    @RequiresPermissions("study:exam:studyQuestionbankType:delete")
    public RespBody<String> deleteStudyQuestionbankType(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        //校验是否绑定题库（开放与未开发都校验）
        QueryWrapper<StudyQuestionbankEntity> wrapper= new QueryWrapper<>();
        wrapper.eq("qbtype_id",id);
//        wrapper.le("open_start_time",new Date());
//        wrapper.ge("open_end_time",new Date());
        List<StudyQuestionbankEntity> list = studyQuestionbankService.list(wrapper);
        if(list!=null && list.size()>0){
            throw new BizException(BizErrorCode.QUESTIONBANK_TYPE_BIND_DEL_ERROR);
        }
        if(studyQuestionbankTypeService.removeById(id)) {
            //删除该分类下的题库资源
            studyQuestionbankService.delByTypeId(id);
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


//    /**
//     * 题库分类批量删除
//     * @param ids
//     * @return
//     */
//    @CastleLog(operLocation="题库分类",operType = OperationTypeEnum.DELETE)
//    @ApiOperation("题库分类批量删除")
//    @PostMapping("/study/exam/studyQuestionbankType/deleteBatch")
//    @ResponseBody
//    @RequiresPermissions("study:exam:studyQuestionbankType:deleteBatch")
//    public RespBody<String> deleteStudyQuestionbankTypeBatch(@RequestBody List<Long> ids){
//        if(ids == null || ids.size()<1){
//            throw new BizException(GlobalRespCode.PARAM_MISSED);
//        }
//        if(studyQuestionbankTypeService.removeByIds(ids)) {
//            return RespBody.data("保存成功");
//        }else{
//            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
//        }
//    }

    /**
     * 题库分类详情
     * @param id 题库分类id
     * @return
     */
    @CastleLog(operLocation="题库分类",operType = OperationTypeEnum.QUERY)
    @ApiOperation("题库分类详情")
    @GetMapping("/study/exam/studyQuestionbankType/info")
    @ResponseBody
    @RequiresPermissions("study:exam:studyQuestionbankType:info")
    public RespBody<StudyQuestionbankTypeDto> infoStudyQuestionbankType(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyQuestionbankTypeEntity studyQuestionbankTypeEntity = studyQuestionbankTypeService.getById(id);
        StudyQuestionbankTypeDto studyQuestionbankTypeDto = ConvertUtil.transformObj(studyQuestionbankTypeEntity,StudyQuestionbankTypeDto.class);

        return RespBody.data(studyQuestionbankTypeDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation="题库分类",operType = OperationTypeEnum.EXPORT)
	@PostMapping("/study/exam/studyQuestionbankType/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<StudyQuestionbankTypeDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<StudyQuestionbankTypeDto> list = studyQuestionbankTypeService.listStudyQuestionbankType(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
